package com.hunzi.interceptor;

import com.hunzi.domain.AdminUser;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminLoginInterceptor implements HandlerInterceptor {

    //实现preHandle()方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println ("拦截页面");

        //当管理员登录成功了才能操作后台数据库
        //1.将管理员从session中取出
        AdminUser adminUser = (AdminUser) request.getSession ().getAttribute ( "adminUser" );
        //判断session中是否存储用户对象
        if (adminUser!=null){
            //放行
            return true;
        }
        //登录失败 拦截 强制跳转到登录页面
        request.setAttribute ( "error" , "请输入账号和密码" );
        request.getRequestDispatcher ( "/admin/login" ).forward ( request , response );
        //返回false表示拦截所有资源
        return false;
    }
}
